package com.gz.purchase.app;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gz.purchase.R;
import com.gz.purchase.adapter.MoveInStorageConfirmAdapter;
import com.gz.purchase.fragment.PurchaseFragment;
import com.gz.purchase.library.IAuthor;
import com.gz.purchase.library.IProduct;
import com.gz.purchase.library.listener.BaseListener;
import com.gz.purchase.library.listener.GetMoveInStorageConfirm;
import com.gz.purchase.library.listener.RejectOrAgreeInStorageListener;
import com.gz.purchase.library.pojo.MoveInStorageItemInfo;
import com.gz.purchase.library.pojo.UserInfo;
import com.gz.purchase.library.util.Constants;
import com.gz.purchase.library.util.PreferenceModel;
import com.gz.purchase.util.ToastUtils;
import com.gz.purchase.widget.LoadingCover;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 移库入库确认 界面
 */
public class MoveInStorageConfirmActivity extends BaseActivity
        implements PullToRefreshBase.OnRefreshListener,LoadingCover.OnLoadingCoverRefreshListener, MoveInStorageConfirmAdapter.OnButtonYesOrNoClickListener, View.OnClickListener {
    private String TAG = MoveInStorageConfirmActivity.class.getSimpleName();
    private ImageView iv_actionbar_up;
    private TextView tv_actionbar_title;
    private TextView tv_actionbar_deliveryName;

    private PullToRefreshListView mPullListView ;
    private LoadingCover  mLoadingCover;
    private Dialog mLoadingData;

    private UserInfo info ;
    private MoveInStorageConfirmAdapter mAdapter;
    private List<MoveInStorageItemInfo> mDataList;
    private MyGetMoveInStorageConfirm myGetMoveInStorageConfirm;
    private MyRejectOrAgreeInStorageListener myRejectOrAgreeInStorageListener;
    private MoveInStorageItemInfo clickItemInfo;// 用来记录当前点击的条目

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_move_in_storage_confirm);
        mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        mActionBar.setCustomView(R.layout.common_activity_head_layout);//自定义ActionBar布局
        initView();
        initListener();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myGetMoveInStorageConfirm.release();
    }

    @Override
    public void onLoadingCoverRefresh() {
        IProduct.getInstance().getInstorageConfirmInfo(myGetMoveInStorageConfirm);
    }

    // ListView 每个条目里 拒绝按钮的点击事件
    @Override
    public void onRefresh(PullToRefreshBase refreshView) {
        IProduct.getInstance().getInstorageConfirmInfo(myGetMoveInStorageConfirm);
    }

    // ListView 每个条目里 确认按钮的点击事件
    @Override
    public void onClickYesBtn(MoveInStorageItemInfo info) {
        if (info != null){
            mLoadingData.show();
            clickItemInfo = info;
            IProduct.getInstance().inStorageRejectOrAgree(Define.AGREE_IN_STORAGE,info.getId(),myRejectOrAgreeInStorageListener);
        }


    }

    @Override
    public void onClickNoBtn(MoveInStorageItemInfo info) {
        if (info != null){
            mLoadingData.show();
            clickItemInfo = info;
            IProduct.getInstance().inStorageRejectOrAgree(Define.REJECT_IN_STORAGE,info.getId(),myRejectOrAgreeInStorageListener);
        }

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.iv_actionbar_up){
            this.finish();
        }
    }

    public void initView(){
        info = IAuthor.getInstance().loadUserInfoSyn(PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
        iv_actionbar_up = (ImageView) this.findViewById(R.id.iv_actionbar_up);
        tv_actionbar_title = (TextView) this.findViewById(R.id.tv_actionbar_title);
        tv_actionbar_deliveryName = (TextView) this.findViewById(R.id.tv_actionbar_deliveryName);

        mPullListView = (PullToRefreshListView) this.findViewById(R.id.list);
        mLoadingCover = (LoadingCover) this.findViewById(R.id.layout_loading_cover);

        mLoadingData = new Dialog(this,R.style.DialogTheme);
        mLoadingData.setContentView(R.layout.loading_data_dialog2);
        mLoadingData.setCanceledOnTouchOutside(false);
        mLoadingData.setCancelable(false);

    }

    public void initListener(){
        myGetMoveInStorageConfirm= new MyGetMoveInStorageConfirm(this);
        myRejectOrAgreeInStorageListener= new MyRejectOrAgreeInStorageListener(this);
        mLoadingCover.setOnLoadingCoverRefreshListener(this);
        mPullListView.setOnRefreshListener(this);
        iv_actionbar_up.setOnClickListener(this);

    }

    public void initData(){
        tv_actionbar_title.setText("入库确认");
        if (info != null){
            tv_actionbar_deliveryName.setText(info.getName() + "：" + info.getUser_name());
        }

        mDataList = new ArrayList<MoveInStorageItemInfo>();
        mAdapter = new MoveInStorageConfirmAdapter(this,mDataList,this);
        mPullListView.setAdapter(mAdapter);
        onLoadingCoverRefresh();
    }

    /**
     * 获取数据回调监听器
     */
    private static class MyGetMoveInStorageConfirm extends GetMoveInStorageConfirm {
        private MoveInStorageConfirmActivity mOuter;

        public MyGetMoveInStorageConfirm(MoveInStorageConfirmActivity mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<MoveInStorageConfirmActivity>(mOuter);
        }

        @Override
        public void onNoData(String msg) {
            mOuter.mLoadingCover.onRefreshFailed(msg);
            if (mOuter.mPullListView.isRefreshing()){
                mOuter.mPullListView.getLoadingLayoutProxy().refreshSuccess();
            }
            ToastUtils.showToast(mOuter,msg);
        }

        @Override
        public void onResponseSuccess(List<MoveInStorageItemInfo> infos) {
            if (infos == null){
                infos = new ArrayList<MoveInStorageItemInfo>();
            }
            mOuter.mDataList.clear();
            mOuter.mDataList.addAll(infos);
            mOuter.mAdapter.notifyDataSetChanged();
            mOuter.mLoadingCover.onRefreshSuccess();
            if (mOuter.mPullListView.isRefreshing()){
                mOuter.mPullListView.getLoadingLayoutProxy().refreshSuccess();
            }

        }

        @Override
        public void onNoNetWork() {
            mOuter.mLoadingCover.onRefreshFailed(Define.MSG_LOADING_BY_CLICK);
            if (mOuter.mPullListView.isRefreshing()){
                mOuter.mPullListView.getLoadingLayoutProxy().refreshFailed();
            }
            ToastUtils.showToast(mOuter,Define.MSG_TOAST_NO_NETWORK);

        }

        @Override
        public void onResponseFailed() {
            mOuter.mLoadingCover.onRefreshFailed();
            if (mOuter.mPullListView.isRefreshing()){
                mOuter.mPullListView.getLoadingLayoutProxy().refreshFailed();
            }
            ToastUtils.showToast(mOuter,Define.MSG_ON_RESPONSE_FAILED);

        }
    }

    /**
     * 移库出库确认 界面 确认或拒绝某记录 回调监听器
     */
    private static class MyRejectOrAgreeInStorageListener extends RejectOrAgreeInStorageListener {
        private MoveInStorageConfirmActivity mOuter;

        public MyRejectOrAgreeInStorageListener(MoveInStorageConfirmActivity mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<MoveInStorageConfirmActivity>(mOuter);
        }
        @Override
        public void onResponsessSuccess() {
            // 拒绝或者确认成功后移除已拒绝或确认的元素，然后刷新界面
            Iterator<MoveInStorageItemInfo> iterator = mOuter.mDataList.iterator();
            while (iterator.hasNext()){
                if (mOuter.clickItemInfo.getStorage_from_id() == iterator.next().getStorage_from_id()){
                    iterator.remove();
                    break;
                }
            }
            mOuter.mAdapter.notifyDataSetChanged();
            mOuter.clickItemInfo = null;
            if (mOuter.mPullListView.isRefreshing()){
                mOuter.mPullListView.getLoadingLayoutProxy().refreshSuccess();
            }
            mOuter.mLoadingData.dismiss();
            ToastUtils.showToast(mOuter, "操作成功");

        }

        @Override
        public void onRejectFail(String msg) {
            mOuter.mLoadingData.dismiss();
            mOuter.clickItemInfo = null;
            ToastUtils.showToast(mOuter,msg);

        }

        @Override
        public void onAgreeFail(String msg) {
            mOuter.mLoadingData.dismiss();
            mOuter.clickItemInfo = null;
            ToastUtils.showToast(mOuter,msg);

        }

        @Override
        public void onNoNetWork() {
            mOuter.mLoadingData.dismiss();
            mOuter.clickItemInfo = null;
            ToastUtils.showToast(mOuter,Define.MSG_TOAST_NO_NETWORK);

        }

        @Override
        public void onResponseFailed() {
            mOuter.mLoadingData.dismiss();
            mOuter.clickItemInfo = null;
            ToastUtils.showToast(mOuter,"操作失败");

        }
    }

}
